//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Text.RegularExpressions;
using Microsoft.Win32;
using System.Collections.Generic;

namespace Microsoft.TridentWordAddIn.Common
{
    public static class StringExtensions
    {
        private static List<string> fileExtensions = new List<string>();

        /// <summary>
        /// Compares the ignore case.
        /// </summary>
        /// <param name="first">The first.</param>
        /// <param name="value">The value.</param>
        /// <returns>bool</returns>
        public static bool CompareIgnoreCase(this string first, string value)
        {
            if (first != null)
            {
                if (string.Compare(first, value, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Compares the specified first string.
        /// </summary>
        /// <param name="first">The first.</param>
        /// <param name="value">The value.</param>
        /// <returns>bool</returns>
        public static bool Compare(this string first, string value)
        {
            if (string.Compare(first, value, StringComparison.Ordinal) == 0)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Determines whether [contains ignore case] [the specified first].
        /// </summary>
        /// <param name="first">The first.</param>
        /// <param name="second">The second.</param>
        /// <returns>
        /// 	<c>true</c> if [contains ignore case] [the specified first]; otherwise, <c>false</c>.
        /// </returns>
        public static bool ContainsIgnoreCase(this string first, string second)
        {
            if (first == null)
            {
                if (second == null)
                {
                    return true;
                }

                return false;
            }

            first = first.ToLower();
            second = second.ToLower();

            return first.Contains(second);
        }

        /// <summary>
        /// Matcheses at least one.
        /// </summary>
        /// <param name="first">The first.</param>
        /// <param name="values">The values.</param>
        /// <returns>bool</returns>
        public static bool MatchesAtLeastOne(this string first, params string[] values)
        {
            foreach (string value in values)
            {
                if (string.Compare(first, value, StringComparison.Ordinal) == 0)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Determines whether [is null or empty] [the specified value].
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>
        /// 	<c>true</c> if [is null or empty] [the specified value]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNullOrEmpty(this string value)
        {
            return string.IsNullOrEmpty(value);
        }

        /// <summary>
        /// Determines whether [is valid file extension] [the specified value].
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>
        /// 	<c>true</c> if [is valid file extension] [the specified value]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsFileExtension(this string value)
        {
            GetAllFileExtensions();
            return fileExtensions.Contains(value.ToLower());
        }

        /// <summary>
        /// Gets all file extensions.
        /// </summary>
        private static void GetAllFileExtensions()
        {
            if (fileExtensions.Count == 0)
            {
                RegistryKey root = Registry.ClassesRoot;
                string[] extensions = root.GetSubKeyNames();
                foreach (string temp in extensions)
                {
                    if (temp.StartsWith("."))
                    {
                        fileExtensions.Add(temp);
                    }
                }
            }
        }
    }
}
